<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/11/19
 * Time: 15:11
 */

namespace Api\Behavior;



use Api\Service\AliBaichuanService;
use Api\Service\JuheSmsService;

/**
 * 发送验证码行为
 * Class MessageSendBehavior
 * @package Api\Behavior
 */
class MessageSendBehavior  extends \Think\Behavior{

    public function run(&$params){

        addLog("SendMessageBehavior",$params,"","[调试]调用发送短消息");

        //如果开启了阿里百川消息服务
        //$service = new AliBaichuanService();

        //聚合短信
        $service = new JuheSmsService();

        $mobile = $params['mobile'];
        $code = $params['code'];
        $domain=  $params['domain'];
        $domain_text=  $params['domain_text']; //文字描述

        //阿里百川格式
        /*$context = array(
            'appname'=>'森森水族',
            'desc'=>$domain_text,
            'code'=>$code,
            'domain'=>$domain,
            'signature_id'=>'1190',
            'template_id'=>'1758',

            'appkey'=>'23286516',
            'secretKey'=>'77805d2a326a04ff8964265709344406',
        );*/

        //聚合格式
        $context = array(
            'desc'=>$domain_text,
            'code'=>$code,
            'domain'=>$domain,
            'tpl_id'=>'14404',

            //'key'=>'39388a9ef117cc01e197403001a2af04',
            'key'=>'39388a9ef117cc01e197403001a2af04',
        );



        $result = $service->sendMessage($mobile,$context);
        $params = $result;
        addLog("SendMessageBehavior",$params,$result,"[调试]调用发送短消息返回结果");

    }



    //开发环境下关闭短信发送
//        $appkey = C('JUHE_API.MSG_APPKEY'); #通过聚合申请到数据的appkey
//        $tpl_id = C('JUHE_API.MSG_TPL_ID');
//
//        $sms_config = array(
//            'type'=>SMSServices::JUHE_SMS,//聚合短信
//            'appkey' => $appkey, //您申请的APPKEY
//            'mobile' => $mobile, //接受短信的用户手机号码
//            'tpl_id' => $tpl_id, //您申请的短信模板ID，根据实际情况修改
//            'content' => '#code#=' . $code //您设置的模板变量，根据实际情况修改
//        );
//            $result = SMSServices::send($sms_config);
//
//            $error_code = "-1";
//            $msg = "";
//            if ($result) {
//                $json_result = json_decode($result, true);
//                $error_code = $json_result['error_code'];
//                $msg = $json_result['reason'];
//
//                if (intval($error_code) == 0) {
//                    //状态为0，说明短信发送成功
//                    $this->apiReturnSuc("短信发送成功,短信ID：" . $json_result['result']['sid']);
//                }
//
//            }
//
//            $this->apiReturnErr("短信发送失败(" . $error_code . ")：" . $msg);


}